package com.example.common.component;

import com.example.common.control.ICallback;
import com.example.common.model.MyResponse;
import com.example.common.model.RsaApiCreateResp;
import com.example.common.network.RSAReq;
import com.example.common.network.RetrofitServiceManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RSACtrlCom {
    private RSAReq rsaReq;

    public RSACtrlCom(){
        rsaReq= RetrofitServiceManager.getInstance().create(RSAReq.class);
    }

    /**
     *
     * @param iCallback 返回类型 RsaApiCreateResp
     */
    public void postRSA(ICallback iCallback){
        Call<MyResponse<RsaApiCreateResp>> call = rsaReq.postRsa();
        call.enqueue(new Callback<MyResponse<RsaApiCreateResp>>() {
            @Override
            public void onResponse(Call<MyResponse<RsaApiCreateResp>> call, Response<MyResponse<RsaApiCreateResp>> response) {
                if(response.isSuccessful()){
                    MyResponse<RsaApiCreateResp> myResponse= response.body();
                    if(myResponse!=null){
                        if(myResponse.getCode()==200){
                            iCallback.onSuccess(myResponse.getData());
                        }else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    }else {
                        iCallback.onFailed("响应体为空");
                    }
                }else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<RsaApiCreateResp>> call, Throwable t) {
                iCallback.onFailed(t.toString());
            }
        });
    }
}
